home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / accessibility / AccessibleStateSet.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.7 KB  |  189 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AccessibleStateSet.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.accessibility;
  16.  
  17. import java.util.Vector;
  18. import java.util.Locale;
  19. import java.util.MissingResourceException;
  20. import java.util.ResourceBundle;
  21.  
  22. /**
  23.  * Class AccessibleStateSet determines a components state set.  The state set
  24.  * of a component is a set of AccessibleState objects and descriptions the
  25.  * current overall state of the object, such as whether it is enabled, 
  26.  * has focus, etc.
  27.  *
  28.  * @see AccessibleState
  29.  *
  30.  * @version     1.9 08/26/98 21:14:00
  31.  * @author      Willie Walker
  32.  */
  33. public class AccessibleStateSet {
  34.  
  35.     /**
  36.      * Each entry in the Vector represents an AccessibleState.
  37.      * @see #add
  38.      * @see #addAll
  39.      * @see #remove
  40.      * @see #contains
  41.      * @see #toArray
  42.      * @see #clear
  43.      */
  44.     protected Vector states = null;
  45.  
  46.     /**
  47.      * Create a new empty state set.
  48.      */
  49.     public AccessibleStateSet() {
  50.         states = null;
  51.     }
  52.  
  53.     /**
  54.      * Create a new state with the initial set of states contained in 
  55.      * the array of states passed in.  Duplicate entries are ignored.
  56.      * @param state an array of AccessibleState describing the state set.
  57.      */
  58.     public AccessibleStateSet(AccessibleState[] states) {
  59.         if (states.length != 0) {
  60.             this.states = new Vector(states.length);
  61.             for (int i = 0; i < states.length; i++) {
  62.                 if (!this.states.contains(states[i])) {
  63.                     this.states.addElement(states[i]);
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Add a new state to the current state set if it is not already
  71.      * present.  If the state is already in the state set, the state
  72.      * set is unchanged and the return value is false.  Otherwise, 
  73.      * the state is added to the state set and the return value is
  74.      * true.
  75.      * @param state the state to add to the state set
  76.      * @return true if state is added to the state set; false if the state set 
  77.      * is unchanged
  78.      */
  79.     public boolean add(AccessibleState state) {
  80.         // [[[ PENDING:  WDW - the implementation of this does not need
  81.         // to always use a vector of states.  It could be improved by
  82.         // caching the states as a bit set.]]]
  83.         if (states == null) {
  84.             states = new Vector();
  85.         }
  86.  
  87.         if (!states.contains(state)) {
  88.             states.addElement(state);
  89.             return true;
  90.         } else {
  91.             return false;
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Add all of the states to the existing state set.  Duplicate entries 
  97.      * are ignored.
  98.      * @param state  AccessibleState array describing the state set.
  99.      */
  100.     public void addAll(AccessibleState[] states) {
  101.         if (states.length != 0) {
  102.             if (this.states == null) {
  103.         this.states = new Vector(states.length);
  104.             }
  105.             for (int i = 0; i < states.length; i++) {
  106.                 if (!this.states.contains(states[i])) {
  107.                     this.states.addElement(states[i]);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Remove a state from the current state set.  If the state is not
  115.      * in the set, the state set will be unchanged and the return value
  116.      * will be false.  If the state is in the state set, it will be removed
  117.      * from the set and the return value will be true.
  118.      *    
  119.      * @param state the state to remove from the state set
  120.      * @return true is the state is in the state set; false if the state set 
  121.      * will be unchanged
  122.      */
  123.     public boolean remove(AccessibleState state) {
  124.         if (states == null) {
  125.             return false;
  126.         } else {
  127.             return states.removeElement(state);
  128.         }
  129.     }
  130.  
  131.     /**
  132.      * Remove all the states from the current state set.
  133.      */
  134.     public void clear() {
  135.         if (states != null) {
  136.             states.removeAllElements();
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Check if the current state is in the state set.
  142.      * @param state the state
  143.      * @return true if the state is in the state set; otherwise false
  144.      */
  145.     public boolean contains(AccessibleState state) {
  146.         if (states == null) {
  147.             return false;
  148.         } else {
  149.             return states.contains(state);
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Returns the current state set as an array of AccessibleState
  155.      * @return AccessibleState array conatining the current state.
  156.      */
  157.     public AccessibleState[] toArray() {
  158.         if (states == null) {
  159.             return new AccessibleState[0];
  160.         } else {
  161.             AccessibleState[] stateArray = new AccessibleState[states.size()];
  162.             for (int i = 0; i < stateArray.length; i++) {
  163.                 stateArray[i] = (AccessibleState) states.elementAt(i);
  164.             }
  165.             return stateArray;
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * Create a localized String representing all the states in the set 
  171.      * using the default locale.
  172.      *
  173.      * @return comma separated localized String
  174.      * @see AccessibleBundle#toDisplayString
  175.      */
  176.     public String toString() {
  177.         String ret = null;
  178.         if ((states != null) && (states.size() > 0)) {
  179.             ret = ((AccessibleState) (states.elementAt(0))).toDisplayString();
  180.             for (int i = 1; i < states.size(); i++) {
  181.                 ret = ret + "," 
  182.                         + ((AccessibleState) (states.elementAt(i))).
  183.                           toDisplayString();
  184.             }
  185.         }
  186.         return ret;
  187.     }
  188. }
  189.